package com.clerkq.FlvPlayer
{
	import com.clerkq.FlvPlayer.event.VideoEvent;
	import com.clerkq.FlvPlayer.event.VideoPlayerEvent;
	import com.clerkq.FlvPlayer.ui.ProgressIndicator;
	import com.library.core.IDisposable;
	import com.library.utils.NumberUtil;
	import com.library.utils.StringUtil;
	import com.library.utils.Tick.Tick;
	import com.library.utils.Tick.TickEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class VideoController extends EventDispatcher implements IDisposable
	{
        private var _video:VideoDisplay;
		private var _player:VideoPlayer;
        private var _decorate:VideoDecorate;
        private var _url:String;
        
        // 1:PreRoll | 2:Video | 3:PostRoll		
        private var _videoMark:int;
        
		public function VideoController(target:IEventDispatcher=null)
		{
			super(target);
			Tick.getInstance().addEventListener(TickEvent.TICK, onUpdate);
			_videoMark = 0;
		}
		public function init():void{
        }
		
		// Event //////////////////
		private function onPlayerEvent(evt:Event):void{
			switch(evt.type)
			{
				case ProgressIndicator.ON_PRESS_PLAYHEAD:
					_video.pauseForSeek();
					break;
				case ProgressIndicator.ON_RELEASE_PLAYHEAD:
					_video.resumeAfterSeek();
					break;
				case ProgressIndicator.ON_DRAG:
					_video.seek(_player.progressBar.dragPercent);
					break;
				case ProgressIndicator.ON_SEEK:
					_video.seek(_player.progressBar.seekPercent);
					break;
				case VideoPlayerEvent.VIDEO_PLAYER_PLAY:
					_video.resume();
					break;
				case VideoPlayerEvent.VIDEO_PLAYER_PAUSE:
					_video.pause();
					break;
				case VideoPlayerEvent.SOUND_CHANGE:
					_video.volume = _player.volumeBar.percent;
					break;
				case VideoPlayerEvent.CLICK_VIDEO:
					// Open new window after click video
					break;
			}
		}
		private function onVideoEvent(evt:Event):void{
			switch(evt.type)
			{
				case VideoEvent.NEW_TIME:
					break;
				case VideoEvent.ON_BUFFER_EMPTY:
					_decorate.showBufferRing();
					break;
				case VideoEvent.ON_BUFFER_FULL:
					_decorate.hideBufferRing();
					break;
				case VideoEvent.ON_PLAY:
					_player.playStatus = true;
		            _video.visible = true;
		            _video.alpha = 1;
					break;
				case VideoEvent.ON_END:
					// Play next video
					_video.close();
					_decorate.hideBufferRing();
					break;
//				case VideoEvent.ON_STOP:
//					_player.playStatus = false;
//					break;
			}
		}

        private function onUpdate(evt:TickEvent):void{
        	try {
        		_player.progressBar.playPercent = _video.percentPlayed;
	            _player.progressBar.bufferPercent = _video.percentLoaded;
	            
	            var playedTime:Number=NumberUtil.formatNumber(_video.percentPlayed * _video.duration, 0);
				var playedStr:String=StringUtil.FormatMinute(playedTime);
				var totalStr:String=StringUtil.FormatMinute(_video.duration);
				_player.progressBar.displayText = playedStr+" / "+totalStr;
				
            } catch(e:Error) {
            };
        }
        
        private function onNewVideoTime(evt:VideoEvent):void{
            //trace(_video.time);
        }   
        
        // Private function /////////////////////////////
        private function restartVideo(videoURL:String):void{
                _video.close();
                _video.play(videoURL);
        }
        
        private function startNewVideo(videoURL:String):void{
            _player.progressBar.reset();           
            _video.visible = false;            
            _video.play(videoURL);
                
            _decorate.showBufferRing();
            //_video.addEventListener(VideoEvent.NEW_TIME, onNewVideoTime);
        }
        
        // Public function //////////////////////////////
        public function pause():void{
            _video.pause();
            _player.playStatus = false;
        }
        
        public function resume():void{
            _video.resume();
            _player.playStatus = true;
        }
        
        public function set url(url:String):void{
        	_url = url;
        	_decorate.title = "title";
        	
        	var _reg:RegExp = /^\s*$/; // Check if the url is "    "			
			
			if(!_reg.test(_url)){
				startNewVideo(_url);
			}
        }
                
		public function get player():VideoPlayer{
            return _player;
        }
        public function set player(player:VideoPlayer):void{
            _player = player;
            _player.progressBar.addEventListener(ProgressIndicator.ON_PRESS_PLAYHEAD, onPlayerEvent);
            _player.progressBar.addEventListener(ProgressIndicator.ON_RELEASE_PLAYHEAD, onPlayerEvent);
            _player.progressBar.addEventListener(ProgressIndicator.ON_DRAG, onPlayerEvent);
            _player.progressBar.addEventListener(ProgressIndicator.ON_SEEK, onPlayerEvent);
            _player.addEventListener(VideoPlayerEvent.SOUND_CHANGE, onPlayerEvent);
            _player.addEventListener(VideoPlayerEvent.VIDEO_PLAYER_PLAY, onPlayerEvent);
            _player.addEventListener(VideoPlayerEvent.VIDEO_PLAYER_PAUSE, onPlayerEvent);
        }
        
        public function set video(video:VideoDisplay):void{
            _video = video;
            
            // Set default value
            _video.volume = _player.volumeBar.percent;
            
            // Add EventListener
            _video.addEventListener(VideoEvent.ON_PLAY, onVideoEvent);
            _video.addEventListener(VideoEvent.ON_STOP, onVideoEvent);
            _video.addEventListener(VideoEvent.ON_END, onVideoEvent);
            _video.addEventListener(VideoEvent.ON_BUFFER_FULL, onVideoEvent);
            _video.addEventListener(VideoEvent.ON_BUFFER_EMPTY, onVideoEvent);
        }
        
        public function set decorate(decorate:VideoDecorate):void{
            _decorate = decorate;
            _decorate.addEventListener(VideoPlayerEvent.CLICK_VIDEO, onPlayerEvent);
        }
        
		public function Dispose():void
		{
			_video.removeEventListener(VideoEvent.ON_PLAY, onVideoEvent);
            _video.removeEventListener(VideoEvent.ON_END, onVideoEvent);
            _video.removeEventListener(VideoEvent.ON_STOP, onVideoEvent);
            _video.removeEventListener(VideoEvent.ON_BUFFER_FULL, onVideoEvent);
            _video.removeEventListener(VideoEvent.ON_BUFFER_EMPTY, onVideoEvent);
            _video.Dispose();
            
            _player.progressBar.removeEventListener(ProgressIndicator.ON_PRESS_PLAYHEAD, onPlayerEvent);
            _player.progressBar.removeEventListener(ProgressIndicator.ON_RELEASE_PLAYHEAD, onPlayerEvent);
            _player.progressBar.removeEventListener(ProgressIndicator.ON_DRAG, onPlayerEvent);
            _player.progressBar.removeEventListener(ProgressIndicator.ON_SEEK, onPlayerEvent);
            _player.removeEventListener(VideoPlayerEvent.SOUND_CHANGE, onPlayerEvent);
            _player.removeEventListener(VideoPlayerEvent.VIDEO_PLAYER_PLAY, onPlayerEvent);
            _player.removeEventListener(VideoPlayerEvent.VIDEO_PLAYER_PAUSE, onPlayerEvent);
            _player.Dispose();
            
            _decorate.removeEventListener(VideoPlayerEvent.CLICK_VIDEO, onPlayerEvent);
            _decorate.dispose();
		}
	}
}